﻿// TestClient.Web.FileAccessService.svc.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.html)
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       FileAccessService.svc.cs
// Author:     HSS\gbanta
// Created:    07/16/2011
// Modified:   07/16/2011
// ----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Activation;
using HSS.Interlink.Web;
using HSS.Logging;

namespace TestClient.Web
{
	[ServiceContract(Namespace = "https://highspeed-solutions.net/hssinterlink")]
	[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
	public class FileAccessService
	{
		string basePath;

		[OperationContract]
		public IEnumerable<InterlinkFile> GetFileList()
		{
			HashSet<InterlinkFile> files = new HashSet<InterlinkFile>();

			try
			{
				basePath = InterlinkUtilities.GetFolder(UploadHandler.FileStoreFolder);

				foreach (var fileName in Directory.EnumerateFiles(basePath))
				{
					FileInfo fi = new FileInfo(fileName);
					files.Add(new InterlinkFile()
					{
						DateModified = fi.LastWriteTime,
						FileName = fi.Name,
						FileSize = fi.Length,
						FileDescription = InterlinkUtilities.GetContentTypeDescription(fi.Name),
						FileType = HSS.Interlink.Web.MimeMapping.FromFileName(fi.Name)
					});
				}
			}
			catch (Exception ex)
			{
				Log.WriteException(ex);
			}

			return files.OrderByDescending((f) => { return f.DateModified; });
		}

		[OperationContract]
		public bool DeleteFile(string fileName)
		{
			try
			{
				basePath = InterlinkUtilities.GetFolder(UploadHandler.FileStoreFolder);
				fileName = Path.Combine(this.basePath, fileName);
				FileInfo fi = new FileInfo(fileName);
				if (fi.Exists)
					fi.Delete();
				return true;
			}
			catch (Exception ex)
			{
				Log.WriteException(ex);
			}
			return false;
		}

	}

	public class InterlinkFile
	{
		public InterlinkFile()
		{

		}

		public string FileName { get; set; }
		public long FileSize { get; set; }
		public DateTime DateModified { get; set; }
		public string FileDescription { get; set; }
		public string FileType { get; set; }

	}
}